package com.css.dzj.dzjportal.modules.complaint.service.impl;

import com.css.dzj.dzjportal.common.util.excel.ExcelData;
import com.css.dzj.dzjportal.common.util.excel.ExportExcelUtils;
import com.css.dzj.dzjportal.modules.complaint.entity.JgWgrTsjbglEntity;
import com.css.dzj.dzjportal.modules.complaint.service.CompaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class CompaintServiceImpl implements CompaintService {
    @Autowired
    private HttpServletRequest request;
    @Override
    public void exportAppealToExcel(HttpServletResponse response, List<JgWgrTsjbglEntity> jgWgrTsjbglEntities) {
        if (jgWgrTsjbglEntities == null || jgWgrTsjbglEntities.size() < 0) {
            throw new RuntimeException("投诉举报集合为空");
        }
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("投诉举报信息表");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("投诉人");
        titles.add("投诉时间");
        titles.add("投诉状态");
        titles.add("联系电话");
        titles.add("性别");
        titles.add("省");
        titles.add("市");
        titles.add("县");
        titles.add("违法活动记录");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (JgWgrTsjbglEntity tsjbglEntity: jgWgrTsjbglEntities) {
            ArrayList<Object> row = new ArrayList<>();
            row.add(tsjbglEntity.getName());
            SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");
            String formaTime = spf.format(tsjbglEntity.getCreatTime());
            row.add(formaTime);
            row.add("已处理");
            row.add(tsjbglEntity.getPhone());
            row.add(tsjbglEntity.getSex());
            row.add(tsjbglEntity.getProvince());
            row.add(tsjbglEntity.getCities());
            row.add(tsjbglEntity.getCounty());
            row.add(tsjbglEntity.getRecords());
            rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(request,response, "投诉举报信息表.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
